/**
 * @file
 * @description axios封装
 * **/

import axios from "axios";
import { message } from "antd";
import store from '../store/doctor'
axios.defaults.baseURL = "http://47.104.71.104:807/";

/**
 * 请求拦截器
 * **/
axios.interceptors.request.use(
  (config) => {
    config.headers.Authorization = store.token;
    return config;
  },
  (eror) => {
    return Promise.reject(eror);
  }
);

/**
 * 相应拦截器
 * **/
axios.interceptors.response.use(
  (res) => {
    const { newtoken } = response.headers;  //最新的token
    const { Result, Success, ErrorMessage } = res.data
    newtoken && store.setToken(newtoken);
    if (Success) {
      return Result;
    }
    message.error(ErrorMessage);
    return Promise.reject(ErrorMessage);
  },
  (error) => {
    const msg = error.toString();
    if (msg.includes("Timeout")) {
      message.warn("亲！请求时间超时，请慢点戳！");
      return Promise.reject(error);
    } else if (msg.includes("Network Error")) {
      message.warn("亲！网络请求失败，请检查网络是否正常");
      return Promise.reject(error);
    } else {
      const { status } = error.response;
      switch (status) {
        case 401:
          message.error("亲！您的登录已过期，请重新登录");
          //
          break;
        case 500:
          message.error("亲！接口请求失败");
          break;
      }
    }
    return Promise.reject(error);
  }
);

export default axios;
